package com.agileai.crm.controller.procustomer;

import java.util.Date;

import com.agileai.crm.bizmoduler.procustomer.OrgInfoManage;
import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.annotation.PageAction;
import com.agileai.hotweb.bizmoduler.core.StandardService;
import com.agileai.hotweb.controller.core.StandardEditHandler;
import com.agileai.hotweb.domain.FormSelectFactory;
import com.agileai.hotweb.domain.core.User;
import com.agileai.hotweb.renders.DispatchRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.RedirectRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.util.DateUtil;

public class OrgInfoCreateClueEditHandler extends StandardEditHandler {
	public OrgInfoCreateClueEditHandler() {
		super();
		this.serviceId = buildServiceId(OrgInfoManage.class);
	}

	public ViewRenderer prepareDisplay(DataParam param) {
		String operaType = param.get(OperaType.KEY);
		if (!isReqRecordOperaType(operaType)) {
			DataRow record = getService().getRecord(param);
			DataParam showParam = new DataParam();
			showParam.put("CUST_ID", "");
			showParam.put("CLUE_LIKEMAN_NAME",
					record.get("ORG_LINKMAN_NAME"));
			String clueconnway ="邮箱："
					+ record.getString("ORG_EMAIL") + " 其他联系方式："
					+ record.getString("ORG_CONTACT_WAY");
			showParam.put("CLUE_CONN_WAY", clueconnway);
			User user = (User) this.getUser();
			showParam.put("CLUE_CREAT_TIME", DateUtil.getDateByType(
					DateUtil.YYMMDDHHMISS_HORIZONTAL, new Date()));
			showParam.put("CLUE_CREATE_MAN", user.getUserId());
			record.putAll(showParam);
			this.setAttributes(record);
		}
		this.setOperaType(operaType);
		processPageAttributes(param);
		return new LocalRenderer(getPage());
	}

	protected void processPageAttributes(DataParam param) {
	 	setAttribute("CLUE_STATE",
                FormSelectFactory.create("CLUE_STATE")
                                 .addSelectedValue(getAttributeValue("CLUE_STATE",
                                                                          "0")));
		User user = (User) this.getUser();
		this.setAttribute("CLUE_CREATE_NAME",
				this.getAttribute("CLUE_CREATE_NAME", user.getUserName()));
		this.setAttribute("CLUE_CREATE_MAN",
				this.getAttribute("CLUE_CREATE_MAN", user.getUserId()));
		String ccrtDate = (String) this.getAttribute("CLUE_CREATE_TIME",
				DateUtil.getDateByType(DateUtil.YYMMDDHHMI_HORIZONTAL,
						new Date()));
		this.setAttribute("CLUE_CREATE_TIME", ccrtDate);
		setAttribute("CLUE_SOURCE", FormSelectFactory.create("CLUE_SOURCE")
				.addSelectedValue(getOperaAttributeValue("CLUE_SOURCE", "")));
	}

	@PageAction
	public ViewRenderer createClue(DataParam param) {
		((OrgInfoManage) getService()).createClueRecord(param);
		return new RedirectRenderer(getHandlerURL("OrgInfoManageList"));
	}
	 @PageAction
	    public ViewRenderer doCreateClueCustomerAction(DataParam param) {
			storeParam(param);
			String a = "ClueInfoCreateCustomerEdit";
			return new DispatchRenderer(getHandlerURL(a) + "&"
					+ OperaType.KEY + "=doCreateClueCustomerAction&comeFrome=doCreateClueCustomerAction");
		}
	public ViewRenderer doBackAction(DataParam param) {
		return new RedirectRenderer(getHandlerURL("OrgInfoManageList"));
	}

	protected StandardService getService() {
		return (StandardService) this.lookupService(this.getServiceId());
	}
}
